// pages/index/activate/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
	data:'',
	baseUrl:app.globalData.baseUrl,
	id:'',
	fapiao:''
  },
  onPreview(e){
  	console.log(e.currentTarget.dataset.src)
  	// var url =  e
  	wx.previewImage({
  	  current: e.currentTarget.dataset.src, // 当前显示图片的 http 链接
  	  urls: [e.currentTarget.dataset.src] // 需要预览的图片 http 链接列表
  	})
  },
  onGetData(id){
	  let that = this;
	  wx.showLoading({
	  	title: '加载中',
	  	mask: true
	  })
	  wx.request({
	  	url: app.globalData.baseUrl + '/api/apply/detail',
	  	method: 'GET',
	  	header: {
	  		'content-type': 'application/x-www-form-urlencoded',
	  		'token': app.globalData.token,
	  	},
		data:{
			id:id
		},
	  	success: function(res) {
	  		wx.hideLoading()
	  		if (res.data.status) {
				if(res.data.data.records.img_urls){
					res.data.data.records.img_urls = res.data.data.records.img_urls.split(',')
				}
				that.setData({
					data:res.data.data.records,
					fapiao: res.data.data.records.invoice_url? res.data.data.records.invoice_url.split('/')[res.data.data.records.invoice_url.split('/').length-1]:''
				})
				
	  		}

	  	
	  	},
	  	fail: function() {
	  		wx.hideLoading()
	  		console.log("请求失败")
	  	}
	  })
  },
  cancelOrder(){
	  var that = this;
	  wx.showLoading({
	  	title: '加载中',
	  	mask: true
	  })
	  wx.request({
	  	url: app.globalData.baseUrl + '/api/apply/cancel',
	  	method: 'GET',
	  	header: {
	  		'content-type': 'application/x-www-form-urlencoded',
	  		'token': app.globalData.token,
	  	},
		data:{
			order_id:that.data.id
		},
	  	success: function(res) {
	  		wx.hideLoading()
	  		if (res.data.status) {
				wx.showModal({
					title: "提示",
					content: '取消成功',
					icon: 'none',
					showCancel:false,
					confirmColor:'#DC5640',
					success(res){
						wx.navigateBack()	
					}
				})
	  		}
	  	},
	  	fail: function() {
	  		wx.hideLoading()
	  		console.log("请求失败")
	  	}
	  })
  },
  onAgainApply(){
	  var that = this;
	  var arrImgs = that.data.data.img_urls;
	  wx.showLoading({
	  	title: '提交中',
	  	mask: true
	  })
	   
	  console.log( that.data.data)
	  wx.request({
	    url: app.globalData.baseUrl + '/api/apply/apply',
	    method: 'POST',
	    header: {
	      'content-type': 'application/x-www-form-urlencoded',
	  	  'token': app.globalData.token,
	    },
	    data: {
			'id':that.data.id,
	       "bao_xian_name": that.data.data.bao_xian_name,
	       "chu_xian_date": that.data.data.chu_xian_date,
			"ding_sun_price": parseFloat(that.data.data.ding_sun_price),
			"car_number": that.data.data.car_number,
			"car_jia_number": that.data.data.car_jia_number,
			"case_number": that.data.data.case_number,		
			"ding_sun_url": that.data.data.ding_sun_url,
			"cancel_case_url": that.data.data.cancel_case_url,
			"img_urls":arrImgs?arrImgs.join(','):''
	    },
	    success: function(res) {  
	      wx.hideLoading()           
	      if(res.data.status){
			  wx.showModal({
			  	title: "提示",
			  	content: res.data.msg,
			  	icon: 'none',
			  	showCancel:false,
			  	confirmColor:'#DC5640',
			  	success(res){
			  		wx.navigateBack()	
			  	}
			  })
	  		
	      }else {
	  		wx.showToast({
	  			title:res.data.msg,
	  			icon: 'none',
	  			duration: 2000,
	  		})
	  	}
	    },
	    fail: function() {
	      wx.hideLoading()
	      console.log("请求失败")
	    }
	  })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	this.setData({
		id:options.id
	})
	this.onGetData(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})